home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_005 / mouse / mouse.c < prev   
C/C++ Source or Header  |  1992-05-06  |  8KB  |  282 lines

  1. /*
  2.  *
  3.  *    DISCLAIMER:
  4.  *
  5.  *    This program is provided as a service to the programmer
  6.  *    community to demonstrate one or more features of the Amiga
  7.  *    personal computer.  These code samples may be freely used
  8.  *    for commercial or noncommercial purposes.
  9.  * 
  10.  *     Commodore Electronics, Ltd ("Commodore") makes no
  11.  *    warranties, either expressed or implied, with respect
  12.  *    to the program described herein, its quality, performance,
  13.  *    merchantability, or fitness for any particular purpose.
  14.  *    This program is provided "as is" and the entire risk
  15.  *    as to its quality and performance is with the user.
  16.  *    Should the program prove defective following its
  17.  *    purchase, the user (and not the creator of the program,
  18.  *    Commodore, their distributors or their retailers)
  19.  *    assumes the entire cost of all necessary damages.  In 
  20.  *    no event will Commodore be liable for direct, indirect,
  21.  *    incidental or consequential damages resulting from any
  22.  *    defect in the program even if it has been advised of the 
  23.  *    possibility of such damages.  Some laws do not allow
  24.  *    the exclusion or limitation of implied warranties or
  25.  *    liabilities for incidental or consequential damages,
  26.  *    so the above limitation or exclusion may not apply.
  27.  *
  28.  */
  29.  
  30. /* mouse.c */
  31.  
  32. /* *********************************************************************** */
  33. /* mouse test, for right game port on the Amiga.
  34.  
  35.    Notes:  The right port is used for this test because the input.device
  36.    task is busy continuously with the lefthand port, feeding input events
  37.    to intuition or console devices.  If Intuition is not activated
  38.    (applications which take over the whole machine may decide not to
  39.    activate Intuition), and if no console.device is activated either,
  40.    the input.device will never activate... allowing the application
  41.    free reign to use either the left OR the right hand joystick/mouse
  42.    port.  If either intuition or the console device are activated,
  43.    the lefthand port will yield, at best, every alternate input
  44.    event to an external application such as this test program.
  45.  
  46.    This will undoubtedly mess up either of the two applications
  47.    and should therefore be avoided.  It was ok to use the right
  48.    port in this case, since the system has no particular interest
  49.    in monitoring it.
  50.  
  51.    Using a function called SetMPort, you can reconfigure so that the
  52.    mouse is expected in the other port, but that isnt demonstrated here.
  53.  
  54.    Author:  Rob Peck, 12/1/85
  55.  
  56.    This code may be freely utilized to develop programs for the Amiga.
  57.  
  58.  
  59.  
  60. *********************************************************************** */
  61.  
  62.  
  63.  
  64. #include <exec/types.h>
  65. #include <exec/devices.h>
  66. #include <graphics/gfx.h>
  67. #include <devices/gameport.h>
  68. #include <devices/inputevent.h>
  69.  
  70. LONG GfxBase=0;
  71.  
  72. #define XMOVE 10
  73. #define YMOVE 10
  74. #define MAX(m,n) (m > n ? m : n)
  75.  
  76. struct GamePortTrigger mousetrigger = {
  77.                     GPTF_UPKEYS + GPTF_DOWNKEYS,
  78.                     1800,
  79.                     XMOVE,
  80.                     YMOVE };
  81.     /* trigger on all mouse key transitions, and every 30 seconds, and
  82.                    for any 10 in an x or y direction */
  83.  
  84. struct InputEvent *game_data;    /* pointer into the returned data area
  85.                      * where input event has been sent */
  86. SHORT         error;
  87.  
  88. struct IOStdReq *game_io_msg;
  89.  
  90. BYTE         gamebuffer[sizeof( struct InputEvent )];
  91. BYTE         *gamedata;
  92.  
  93. SHORT         testval;
  94.     
  95. struct MsgPort     *game_msg_port;
  96.  
  97. SHORT movesize;
  98. extern struct MsgPort *CreatePort();
  99. extern struct IOStdReq *CreateStdIO();        
  100.  
  101. SHORT codeval, timeouts;
  102.  
  103. #define IF_NOT_IDLE_TWO_MINUTES while(timeouts < 4)
  104.  
  105. main()
  106. {
  107.  
  108.     GfxBase = OpenLibrary("graphics.library", 0);
  109.     if (GfxBase == NULL)
  110.     {
  111.         printf("Unable to open graphics library\n");
  112.         exit(1000);
  113.     }
  114.     printf(" Mouseport Demo\n");
  115.     printf("\nMove Mouse from Left Port to Right Port\n");
  116.     printf("\nThen move the mouse and click its buttons");
  117.  
  118.     
  119.     timeouts = 0;
  120.  
  121.     gamedata = &gamebuffer[0];    
  122.         /* point to first location in game buffer */
  123.  
  124.     game_msg_port = CreatePort(0,0);    
  125.         /* provide a port for the IO response */
  126.     if(game_msg_port == 0)
  127.         {
  128.         printf("\nError While Performing CreatePort");
  129.         exit(-1);
  130.         }
  131.  
  132.     game_io_msg = CreateStdIO(game_msg_port);     
  133.         /* make an io request block for communicating with
  134.                 the keyboard */
  135.  
  136.     if(game_io_msg == 0)
  137.         {
  138.         printf("\nError While Performing CreateStdIO");
  139.         DeletePort(game_msg_port);
  140.         exit(-2);
  141.         }
  142.  
  143.     error = OpenDevice("gameport.device",1,game_io_msg,0);
  144.         /* open the device for access, unit 1 is right port */
  145.  
  146.     if(error != 0)
  147.         {
  148.         printf("\nError while opening the device, exiting");
  149.         DeleteStdIO(game_io_msg);
  150.         DeletePort(game_msg_port);
  151.         exit(-3);
  152.         }
  153.  
  154.     game_io_msg->io_Length = sizeof(struct InputEvent);    
  155.         /* read one event each time we go back to the gameport */
  156.  
  157.     game_io_msg->io_Data = (APTR)gamebuffer;    
  158.         /* show where to put the data when read */
  159.  
  160.     game_data = (struct InputEvent *)gamebuffer;
  161.  
  162.         /* test the mouse in this loop */
  163.     
  164.     set_controller_type(GPCT_MOUSE);
  165.  
  166.     game_io_msg->io_Command = GPD_SETTRIGGER;    
  167.         /* specify the trigger conditions */
  168.     game_io_msg->io_Data = (APTR)&mousetrigger;    
  169.         /* show where to find the trigger condition info */
  170.         /* this command doesn't wait... returns immediately */
  171.     SendIO(game_io_msg);    
  172.     WaitPort(game_msg_port);
  173.     GetMsg(game_msg_port);
  174.     printf("\nI will report:");
  175.     printf("\n     Mouse X or Y moves if either is over 10 counts");
  176.     printf("\n     Button presses (along with mouse moves if any)");
  177.     printf("\n     Or every 30 seconds (along with mouse moves if any)");
  178.     printf("\n          if neither move or click happens\n");
  179.  
  180.     printf("\nIf no activity for 2 minutes, the program exits\n");
  181.     game_io_msg->io_Command = GPD_READEVENT;    
  182.         /* from now on, just read input events */
  183.     game_io_msg->io_Data = (APTR)gamebuffer;        
  184.         /* into the input buffer, one at a time. */
  185.         /* read-event waits for the preset conditions */
  186.  
  187.     IF_NOT_IDLE_TWO_MINUTES
  188.     {
  189.     game_io_msg->io_Length = sizeof(struct InputEvent);    
  190.         /* read one event each time we go back to the gameport */
  191.  
  192.     printf("\n Waiting For Mouse Report\n");
  193.     SendIO(game_io_msg);
  194.     WaitPort(game_msg_port);
  195.         /* this is NOT a busy wait... it is a task-sleep */
  196.     GetMsg(game_msg_port);
  197.  
  198.     codeval = game_data->ie_Code;
  199.     switch(codeval) 
  200.         {
  201.         case IECODE_LBUTTON:
  202.             printf("\nMouse Left Button Pressed");
  203.             maybe_mouse_moved();
  204.             break;
  205.         case IECODE_RBUTTON:
  206.             printf("\nMouse Right Button Pressed");
  207.             maybe_mouse_moved();
  208.             break;
  209.         case (IECODE_LBUTTON + IECODE_UP_PREFIX):
  210.             printf("\nMouse Left Button Released");
  211.             maybe_mouse_moved();
  212.             break;
  213.         case (IECODE_RBUTTON + IECODE_UP_PREFIX):
  214.             printf("\nMouse Right Button Released");
  215.             maybe_mouse_moved();
  216.             break;
  217.         case IECODE_NOBUTTON:
  218.             timeouts++;    /* after 2 minutes, dump program 
  219.                      * if user loses interest 
  220.                      */
  221.             movesize = maybe_mouse_moved();
  222.             if(movesize == 0)
  223.                printf("\n30 seconds passed, no trigger events");
  224.             else if(movesize < XMOVE && movesize < YMOVE )
  225.                {
  226.                printf("\n(Even though less than trigger count,");
  227.                printf("\n reporting mouse move at the selected");
  228.                printf("\n timing interval for user info)");
  229.                }
  230.             break;
  231.         default:
  232.             break;
  233.         }
  234.     }
  235.  
  236.     set_controller_type(GPCT_NOCONTROLLER);
  237.  
  238.     CloseDevice(game_io_msg);
  239.     DeleteStdIO(game_io_msg);
  240.     DeletePort(game_msg_port);
  241.     
  242.     printf("\nExiting program... 2 minutes with no activity sensed\n1> ");
  243.     return(0);
  244. }        
  245.  
  246.     /* if mouse didnt move far enough to trigger a report, then caller 
  247.      * will also report that 30 seconds (1800 vblanks) has elapsed 
  248.      */
  249.  
  250. int maybe_mouse_moved()        
  251. {
  252.     int xmove, ymove;
  253.     xmove = game_data->ie_X;
  254.     ymove = game_data->ie_Y;
  255.  
  256.     if(xmove != 0 || ymove != 0)
  257.         {
  258.         printf("\nMouse Moved by X-value %ld, Y-value %ld",
  259.             xmove, ymove);
  260.         timeouts = 0;
  261.         }
  262.     if(xmove < 0) xmove = -xmove;
  263.     if(ymove < 0) ymove = -ymove;
  264.  
  265.     return(MAX(xmove,ymove));
  266. }
  267.  
  268. int set_controller_type(type)
  269. SHORT type;
  270. {
  271.     game_io_msg->io_Command = GPD_SETCTYPE;   
  272.         /* set type of controller to mouse */
  273.     *gamedata = type;    
  274.         /* this means dont generate any more reports */
  275.     SendIO(game_io_msg);    
  276.         /* set it up */
  277.         /* this command doesn't wait... returns immediately */
  278.     WaitPort(game_msg_port);
  279.     GetMsg(game_msg_port);
  280.     return(0);
  281. }
  282.